package com.atguigu.day22.tcpsocket.s1;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
    public static void main(String[] args) throws Exception {
        System.out.println("------------Server-------------");
        //1.创建服务端对象
        ServerSocket server = new ServerSocket(9999);
        //2.获取连接的客户端socket
        Socket socket = server.accept();
        //3.获取字节输入流
        InputStream is = socket.getInputStream();
        //4.创建Scanner
        Scanner sc = new Scanner(is);
        //5.读取数据并展示
        String s = sc.nextLine();
        System.out.println("客户端发来的信息是 = " + s);

        //获取字节输出流
        OutputStream os = socket.getOutputStream();
        //转为打印流
        PrintStream ps = new PrintStream(os);
        //写出数据
        ps.println("我很好 你呢 我的朋友");

        //6.关闭资源
        sc.close();
        ps.close();
        socket.close();
        server.close();
    }
}
